/*    

    bobbietalk - a visual programming environment for toy robotics

    Copyright (C) 2013  Roberto Tiella

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/
package bobbietalk.gui.dialogs;

import bobbietalk.gui.blocks.parametrized.EnumTypeSpec;
import bobbietalk.gui.blocks.parametrized.IntTypeSpec;
import bobbietalk.gui.blocks.parametrized.ParametrizedBlock;
import bobbietalk.gui.blocks.parametrized.TypeSpec;
import java.awt.Component;

/**
 *
 * @author roberto
 */
public class BlockEditingDialog extends javax.swing.JDialog {

    private boolean userConfirmed;

    /**
     * Creates new form UserInputDialog
     */
    public BlockEditingDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();

    }

    public void setBlock(ParametrizedBlock block) {

        setTitle(block.getTypeLabel().replaceAll("<[^>]*>", " "));
        attributesPanel.removeAll();

        for (String attrName : block.getAttributeNames()) {

            if (block.isAttributeEditable(attrName)) {

                TypeSpec attrType = block.getAttributeType(attrName);

                if (attrType.getName().equals("int") || attrType.getName().equals("string")) {
                    AttributePanel ap = new AttributePanel();
                    ap.setAttributeName(attrName);
                    ap.setAttributeLabel(block.getAttributeLabel(attrName));
                    ap.setUnits(block.getUnits(attrName));
                    ap.setAttributeValue(block.getAttributeValueAsString(attrName));
                   
                    if (attrType.getName().equals("int")) {
                        
                        int min = ((IntTypeSpec)attrType).getMinValue();
                        int max = ((IntTypeSpec)attrType).getMaxValue();
                        
                        IntegerRangeVerifier verifier = new IntegerRangeVerifier(min,max,this);
                        
                        ap.setInputVerifier(verifier);
                        
                    }
                    
                    
                    attributesPanel.add(ap);
                    
                   

                } else if (attrType.getName().equals("enum")) {

                    EnumTypeSpec eType = (EnumTypeSpec) attrType;

                    EnumAttributePanel ap = new EnumAttributePanel();

                    ap.setAttrbuteEnumType(eType);
                    ap.setAttributeName(attrName);
                    ap.setAttributeLabel(block.getAttributeLabel(attrName));
                    ap.setUnits(block.getUnits(attrName));
                    ap.setAttributeValue(block.getAttributeValueAsString(attrName));

                    attributesPanel.add(ap);

                }



            }

        }

        pack();

    }

    public boolean isUserConfirmed() {
        return userConfirmed;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        attributesPanel = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("bobbietalk/Labels"); // NOI18N
        okButton.setText(bundle.getString("dialog.editblock.ok.label")); // NOI18N
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        cancelButton.setText(bundle.getString("dialog.editblock.cancel.label")); // NOI18N
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        attributesPanel.setLayout(new java.awt.GridLayout(0, 1));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(attributesPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(cancelButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 148, Short.MAX_VALUE)
                        .addComponent(okButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(attributesPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(okButton)
                    .addComponent(cancelButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed

        userConfirmed = true;
        this.setVisible(false);

    }//GEN-LAST:event_okButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        userConfirmed = false;
        this.setVisible(false);
    }//GEN-LAST:event_cancelButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel attributesPanel;
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton okButton;
    // End of variables declaration//GEN-END:variables

    public void saveBlock(ParametrizedBlock block) {

        for (int k = 0; k < attributesPanel.getComponentCount(); k++) {

            Component component = attributesPanel.getComponent(k);

            if (component instanceof AttributePanel) {

                AttributePanel ap = (AttributePanel) component;

                String an = ap.getAttributeName();

                block.setAttributeValueFromString(an, ap.getAttributeValue());

            } else if (component instanceof EnumAttributePanel) {

                EnumAttributePanel ap = (EnumAttributePanel) component;

                String an = ap.getAttributeName();

                block.setAttributeValueFromString(an, ap.getAttributeValue());

            }

        }


    }

    public void setOkButtonEnabled(boolean res) {
       
        okButton.setEnabled(res);
        
    }
}
